package com.controller.front;

import com.entity.Address;
import com.entity.User;
import com.service.AddressService;
import com.sun.org.apache.xpath.internal.operations.Mod;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
@RequestMapping("front/address")
public class AddressController {
    @Autowired
    private AddressService addressService;
    @RequestMapping("showAddress")
    public String showAddress(Integer user_id, Model model){
        List<Address> address = addressService.selectAddress(user_id);
        model.addAttribute("address",address);
        return "forward:/front/order/place-order.jsp";
    }
    @RequestMapping("addAddress")
    public String addAddress(Address address, HttpSession session){
        User user = (User)session.getAttribute("user");
        address.setUser_id(user.getUser_id());
        addressService.insertAddress(address);
        return "redirect:/front/address/showAddress.do?user_id="+user.getUser_id();
    }
    @RequestMapping("removeAddress")
    public String removeAddress(Integer id,HttpSession session){
        User user = (User)session.getAttribute("user");
        addressService.deleteAddressById(id);
        return "redirect:/front/address/showAddress.do?user_id="+user.getUser_id();
    }
    @RequestMapping("updateAddress")
    public String updateAddress(HttpSession session,Integer id,String name,String mobile,String province,
                                String city,String district,String town,String address){
        User user = (User)session.getAttribute("user");
        Integer userId = user.getUser_id();
        Address address1 = new Address(id, name, mobile, province, city, district, town, address, userId);
        addressService.updateAddress(address1);
        System.out.println("address = "+address);
        return "redirect:/front/address/showAddress.do?user_id="+user.getUser_id();
    }
}
